package com.dpxm.zdbdg.charts.shuangseqiu.service;

import com.alibaba.fastjson.JSONObject;
import com.dpxm.zdbdg.charte.sand.vo.PtQueryDPaiLie3VO;
import com.dpxm.zdbdg.charts.shuangseqiu.dao.KuaiLeBaDao;
import com.dpxm.zdbdg.charts.shuangseqiu.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import vehiclebase.utils.ToolsUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class KuaiLeBaService {
    @Autowired
    private KuaiLeBaDao kuaiLeBaDao;
    @Autowired
    private KuaiLeBaSiDanAll kuaiLeBaSiDanAll;

    static int cnt = 0;

    public String saveKuaiLeBa(PtKuaiLeBaVo ptQuerKuaiLe) {
        //快乐8官网 对象
        PtKuaiLeBaVo ptQuerKuaiLeBa = new PtKuaiLeBaVo();
      //  RestTemplate restTemplate = new RestTemplate();
       // String kuaiLeBaLiShi = restTemplate.getForObject("http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=kl8&issueCount=&issueStart=&issueEnd=&dayStart=2021-01-01&dayEnd=2021-03-01", String.class);
        //将官网的数据转化为自有对象
     //   ptQuerKuaiLeBa = JSONObject.parseObject(kuaiLeBaLiShi, PtKuaiLeBaVo.class);

        if(ToolsUtils.notEmpty(ptQuerKuaiLe)){
            //将数据存入本地库
            List<result> result = ptQuerKuaiLe.getResult();
            //存入数据
            kuaiLeBaDao.saveKuaiLeBa(result);
        }

        return "";
    }

    public String selectKuaiLeBa() {
        KuaiLeBaSidan kuaiLeBaSidans = new KuaiLeBaSidan();

         Stack<Integer> stack = new Stack<Integer>();
         List<String> list = new ArrayList<>(); //组合出来的四位数的集合
        //查询出历史最新一期的号码集合
        List<KuaiLeBaVO> kuaiLeBaVoList = kuaiLeBaDao.QueryMessage();
        kuaiLeBaVoList.forEach(a->{
            List<KuaiLeBaSidan> KuaiLeBaSidanList= new ArrayList<>();
            String numbStr = a.getNubm(); //取到最新的开奖号码
            List<String> listnNumbe = Arrays.asList(numbStr.split(","));
            //List<String> 转化为List<Integer>
            List<Integer> codesInteger = listnNumbe.stream().map(Integer::parseInt).collect(Collectors.toList());
            /*
            * codesInteger 上标
            * maxnum下标
            * curnum 对比的参数
            * indexnum 用于后续的循环
            * curnum和indexnum相同时 cnt加一次即存一次数据
            * */
            recursion3(codesInteger, 0, 4, 0,cnt,stack,list,a.getQihao(),KuaiLeBaSidanList,a.getCreatedate());
          //  System.out.println(cnt);
          //  System.out.println(list.size());
            //存入数据
            try {
                //更具期号查询有无此条数据，有则不存储
                KuaiLeBaSidanVO kuaiLeBaVO = kuaiLeBaDao.queryQiHaoMessage(a.getQihao());
               if(ToolsUtils.isEmpty(kuaiLeBaVO)){
                   kuaiLeBaDao.saveKuaiLeBaSiDan(KuaiLeBaSidanList);
               }
            } catch (Exception e) {

            }
        });

        return "";
    }
    ///maxnum 标识位数
    public  void recursion3(List<Integer> array, int curnum, int maxnum, int indexnum,
                                  int cnt,Stack<Integer>  stack,List<String> list,String qihao, List<KuaiLeBaSidan> kuaiLeBaSidans,String createdate ) {
        if (curnum == maxnum) {
            cnt++;
          //  System.out.println(stack);
          //  System.out.println("取数"+stack.get(0)+","+stack.get(1)+","+stack.get(2)+","+stack.get(3));
            if(ToolsUtils.notEmpty(qihao)){
                KuaiLeBaSidan kuaiLeBaSidan = new KuaiLeBaSidan();
                kuaiLeBaSidan.setQihao(qihao);
                kuaiLeBaSidan.setCreatedate(createdate);
                kuaiLeBaSidan.setNubm(stack.get(0)+","+stack.get(1)+","+stack.get(2)+","+stack.get(3));
                kuaiLeBaSidans.add(kuaiLeBaSidan);
            }

            return;
        }
        for (int i = indexnum; i < array.size(); i++) {
            if (!stack.contains(array.get(i))) {
                stack.push(array.get(i));
                recursion3(array, curnum + 1, maxnum, i,cnt,stack,list,qihao,kuaiLeBaSidans,createdate);
                stack.pop();
            }
        }
    }

    public KbSiDanRes selectKuaiLeBaSiDan() {
        KbSiDanRes kbSiDanRes = new KbSiDanRes();
        /*查所有的四胆组合*/
        List<String> listNUmbALL= kuaiLeBaSiDanAll.saveKuaiLeBaAll();
        /*查询至今已出现过的四胆*/
        List<String> listNubmSiDan = kuaiLeBaDao.QueryMessageAll();
        //最近的号码需要先去重复
        List<String> nubmList=listNubmSiDan.stream().distinct().collect(Collectors.toList());
        // 得所有的四胆组合没有开过的号码
        List<String> Numbs = listNUmbALL.stream().filter(a->!nubmList.contains(a)).collect(Collectors.toList());
        kbSiDanRes.setNoSiDanNumb(Numbs);
        return kbSiDanRes;
    }

}
